#include "course.h"
#include <QStringList>
#include <qDebug>

Course::Course()
{

}

Course::Course(int id, QString name, CourseAttribute sx, CourseNature xz, double xf, int xs, QString time)
{
    this->m_id = id;
    this->m_name = name;
    this->m_sx = sx;
    this->m_xz = xz;
    this->m_xf = xf;
    this->m_xs = xs;
    this->m_time = time;
}

Course::Course(int id, QString name, QString sx, QString xz, double xf, int xs, QString time)
{
    this->m_id = id;
    this->m_name = name;
    this->m_sx = convertToCourseAttribute(sx);
    this->m_xz = convertToCourseNature(xz);
    this->m_xf = xf;
    this->m_xs = xs;
    this->m_time = time;
}

Course::~Course()
{

}

CourseAttribute Course::convertToCourseAttribute(QString sx)
{
    if (sx == "必修") {
        return CourseAttribute::BiXiu;
    } else if (sx == "限选") {
        return CourseAttribute::XianXuan;
    } else if (sx == "任选") {
        return CourseAttribute::RenXuan;
    } else if (sx == "公选") {
        return CourseAttribute::GongXuan;
    } else if (sx == "选修") {
        return CourseAttribute::XuanXiu;
    } else if (sx == "其他") {
        return CourseAttribute::QiTa;
    } else {
        return CourseAttribute::None;
    }
}

QString Course::convertToString(CourseAttribute sx)
{
    switch (sx) {
    case CourseAttribute::BiXiu:
        return "必修";
        break;
    case CourseAttribute::XianXuan:
        return "限选";
        break;
    case CourseAttribute::RenXuan:
        return "任选";
        break;
    case CourseAttribute::GongXuan:
        return "公选";
        break;
    case CourseAttribute::XuanXiu:
        return "选修";
        break;
    case CourseAttribute::QiTa:
        return "其他";
        break;
    default:
        return "无";
        break;
    }
}

CourseNature Course::convertToCourseNature(QString xz)
{
    if (xz == "其他类别") {
        return CourseNature::QiTaLeiBie;
    } else if (xz == "专业选修课") {
        return CourseNature::ZhuanYeXuanXiuKe;
    } else if (xz == "通识公共选修课") {
        return CourseNature::TongShiGongGongXuanXiuKe;
    } else if (xz == "通识必修课") {
        return CourseNature::TongShiBiXiuKe;
    } else if (xz == "跨专业必修课") {
        return CourseNature::KuaZhuanYeBiXiuKe;
    } else if (xz == "专业必修课") {
        return CourseNature::ZhuanYeBiXiuKe;
    } else if (xz == "学科基础必修课") {
        return CourseNature::XueKeJiChuBiXiuKe;
    } else if (xz == "集中实践教学环节") {
        return CourseNature::JiZhongShiJianJiaoXueHuanJie;
    } else if (xz == "其他") {
        return CourseNature::QiTa;
    } else {
        return CourseNature::None;
    }
}

QString Course::convertToString(CourseNature xz)
{
    switch (xz) {
    case CourseNature::QiTaLeiBie:
        return "其他类别";
        break;
    case CourseNature::ZhuanYeXuanXiuKe:
        return "专业选修课";
        break;
    case CourseNature::TongShiGongGongXuanXiuKe:
        return "通识公共选修课";
        break;
    case CourseNature::TongShiBiXiuKe:
        return "通识必修课";
        break;
    case CourseNature::KuaZhuanYeBiXiuKe:
        return "跨专业必修课";
        break;
    case CourseNature::ZhuanYeBiXiuKe:
        return "专业必修课";
        break;
    case CourseNature::XueKeJiChuBiXiuKe:
        return "学科基础必修课";
        break;
    case CourseNature::JiZhongShiJianJiaoXueHuanJie:
        return "集中实践教学环节";
        break;
    case CourseNature::QiTa:
        return "其他";
        break;
    default:
        return "无";
        break;
    }
}

void Course::setId(int id)
{
    this->m_id = id;
}

void Course::setName(QString name)
{
    this->m_name = name;
}

void Course::setSX(CourseAttribute sx)
{
    this->m_sx = sx;
}

void Course::setSX(QString sx)
{
    this->m_sx = convertToCourseAttribute(sx);
}

void Course::setXZ(CourseNature xz)
{
    this->m_xz = xz;
}

void Course::setXZ(QString xz)
{
    this->m_xz = convertToCourseNature(xz);
}

void Course::setXF(double xf)
{
    this->m_xf = xf;
}

void Course::setXS(int xs)
{
    this->m_xs = xs;
}

void Course::setTime(QString time)
{
    this->m_time = time;
}

int Course::getId()
{
    return this->m_id;
}

QString Course::getName()
{
    return this->m_name;
}

CourseAttribute Course::getSX()
{
    return this->m_sx;
}

QString Course::getSX_str()
{
    return convertToString(this->m_sx);
}

CourseNature Course::getXZ()
{
    return this->m_xz;
}

QString Course::getXZ_str()
{
    return convertToString(this->m_xz);
}

double Course::getXF()
{
    return this->m_xf;
}

int Course::getXS()
{
    return this->m_xs;
}

QString Course::getTime()
{
    return this->m_time;
}

QString Course::toString()
{
    QString str = "【编号:" + QString::number(this->m_id)
                  + "   名称:" + this->m_name
                  + "   属性:" + convertToString(this->m_sx)
                  + "   性质:" + convertToString(this->m_xz)
                  + "   学分:" + QString::number(this->m_xf)
                  + "   学时:" + QString::number(this->m_xs)
                  + "   开设时间:" + this->m_time
                  + "】";
    // qDebug()<<str;
    return str;
}

bool Course::loadCourse(const int id, const QString &str)
{
    QStringList list = str.split("，");

    if (list.isEmpty() || list.length() < 6) {
        return false;
    }

    try {
        this->m_id = id;
        this->m_name = list[0];
        this->m_xz = convertToCourseNature(list[1]);
        this->m_sx = convertToCourseAttribute(list[2]);
        this->m_xf = list[3].split(" ")[1].toDouble();
        this->m_xs = list[4].split(" ")[1].toInt();
        this->m_time = list[5];
    } catch (...) {
        return false;
    }

    return true;
}




